<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  设计模式1：工厂模式

  <script>
    // 简单工厂模式 
    /* 
    function CreatePerson(name, age, sex) {
      this.name = name;
      this.age = age;
      this.sex = sex
    }
    new CreatePerson();
 */
    
    // 复杂工厂模式 
    // 1.父类：一个抽象类，不可以直接实例
    // 2.子类实现自身的实例方法
    function BicycleShop(name) {
      this.name = name
      this.method = function() {
        return this.name
      };
    }

    BicycleShop.prototype = {
      constructor: BicycleShop,
      sellBicycle: function() {
        var bicycle = this.createBicycle();

        bicycle.a()
        bicycle.b()
        return bicycle
      },
      createBicycle: function() {
        throw new Error("父类不能直接实例，需要子类来实例")
      }
    }
    // 寄生组合继承
    function extend(Sub, Sup) {
      var F = function(){};
      F.prototype = Sup.prototype;
      Sub.prototype = new F()
      Sub.prototype.constructor = Sub
      Sub.Sup = Sup.prototype
    }

    function BicycleChild(name) {
      this.name = name
      BicycleShop.call(this.name)
    }

    extend(BicycleChild, BicycleShop)

    BicycleChild.prototype.createBicycle = function() {
      var a = function() {
        console.log('执行a任务');
      }
      var b = function() {
        console.log('执行b任务');
      }
      return {
        a,
        b
      }
    }

    var bicycleChild = new BicycleChild('凤凰')
    console.log(bicycleChild);
    console.log(bicycleChild.createBicycle());



  </script>
</body>
</html>